package com.ydm.fire_fighting_web.modules.ucenter.service.impl;


import com.ydm.fire_fighting_web.entity.ucenter.Result;
import com.ydm.fire_fighting_web.entity.ucenter.UserInfo;
import com.ydm.fire_fighting_web.modules.ucenter.mapper.UserInfoRepository;
import com.ydm.fire_fighting_web.modules.ucenter.mapper.UserMapper;
import com.ydm.fire_fighting_web.modules.ucenter.service.UserService;
import com.ydm.fire_fighting_web.util.Md5Util;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.text.SimpleDateFormat;
import java.util.Date;

@Service
@Transactional(rollbackFor = RuntimeException.class)
public class UserServiceImpl implements UserService {
    @Autowired
    private UserMapper userMapper;
    @Autowired
    private UserInfoRepository userInfoRepository;
    @Autowired
    private HttpServletRequest request;
    /**
     * 注册
     * @param userInfo 参数封装
     * @return Result
     */
    @Transactional
    public Result regist(UserInfo userInfo){
        Result result = new Result();
        result.setSuccess(false);
        result.setDetail(null);
        try {
            //UserInfo existUser = userMapper.findUserByName(userInfo.getUsername());
            UserInfo existUser = userInfoRepository.findByUsername(userInfo.getUsername());
            if(existUser != null){
                //如果用户名已存在
                result.setMsg("用户名已存在");
            }else{
                SimpleDateFormat simpleDateFormat = new SimpleDateFormat("YYYY-MM-dd HH:mm:ss");
                String date = simpleDateFormat.format(new Date());
                userInfo.setCreateTime(date);
                String password = userInfo.getPassword();
                String encodeByMd5 = Md5Util.encodeByMd5(password);
                userInfo.setPassword(encodeByMd5);
                userInfoRepository.save(userInfo);
                result.setMsg("注册成功");
                result.setSuccess(true);
                result.setDetail(userInfo);
            }
        } catch (Exception e) {
            result.setMsg(e.getMessage());
            e.printStackTrace();
        }
        return result;
    }
    /**
     * 登录
     * @param userInfo 用户名和密码
     * @return Result
     */
    public Result login(UserInfo userInfo) {
        Result result = new Result();
        result.setSuccess(false);
        result.setDetail(null);
        try {
            String password = userInfo.getPassword();
            String encodeByMd5 = Md5Util.encodeByMd5(password);
            userInfo.setPassword(encodeByMd5);
            UserInfo userInfo1 = userInfoRepository.findByUsernameAndPassword(userInfo.getUsername(), userInfo.getPassword());
            if(userInfo1==null){
                result.setMsg("用户名或密码错误");
            }else{
                result.setMsg("登录成功");
                result.setSuccess(true);
                result.setDetail(userInfo1);
                request.getSession().setAttribute("user",userInfo1);
            }
        } catch (Exception e) {
            result.setMsg(e.getMessage());
            e.printStackTrace();
        }
        return result;
    }
}
